
<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of locationController
 *
 * @author luigi.pastore
 * @created  11-mag-2011
 */
class LocationController extends Classes_Controller
{
    public function init()
    {
	$this->model_location	= new Classes_Location();
	$this->model_province	= new Classes_Province();
	
	$this->contact_roles		= $this->application->getResource( 'ContactRoles' );
	$this->layout->contact_roles	= $this->contact_roles;
	
	$this->layout->province = $this->model_province->fetchAll();

        // Dati Form
        $this->form_data = $this->getParams();
        
	// Edit...
	if( is_numeric( $id = $this->getParam( 'id' ) ) )
	{
	    // Fetch
	    $location = $this->model_location->fetchById( array( $id ) );

	    if( empty( $location ) )
		trigger_error( 'Nessuna Location' );
	    
	    $this->location = array_shift( $location );

            // Se sto editando precompilo la sessione
            if( !empty( $this->form_data['salva'] ) )
                $this->location = array_merge( $this->location, $this->form_data );
            
//            if( !empty( $this->location ) AND empty( $this->form_data['salva'] ) )
//	    {
//                $_SESSION['Location'] = $this->location;
//		# Formattiamo campi
//		//list( $this->post['post_published_d'], $this->post['post_published_m'], $this->post['post_published_y'] ) = explode( '-', $this->post['post_published_ita'] );
//		//$_SESSION['Post']['Info'] = $this->post;
//            }
//            else
//            {
//                $this->location = array_merge( $this->location, $this->form_data );
//            }
            

	    # Assegniamo i dati al layout
	    $this->layout->data = $this->location;

	    $this->layout->location_id = $this->location['location_id'];
	}           
    }
    
    public function indexAction()
    {
	$this->layout->content = array( 'script' => 'location/index' );
    }
    
    public function infoAction()
    {
        if( $this->form_data['salva'] AND $this->form_data['salva'] !== '' )
	{
	    $this->layout->data = $this->form_data;

            # Mapping...
            $data = array(
                'location_id'		    => $this->location['location_id'],
                'location_name'		    => $this->form_data['location_name'],
                'location_desc'		    => $this->form_data['location_desc'],
                'location_city'		    => $this->form_data['location_city'],
                'location_cap'		    => $this->form_data['location_cap'],
                'location_loc'		    => $this->form_data['location_loc'],
                'location_street'	    => $this->form_data['location_street'],
                'location_street_number'    => $this->form_data['location_street_number'],
                'location_province_id'	    => $this->form_data['location_province_id']
            );

            // Edit o Aggiunta?
            if( empty( $this->location ) )
                $data['location_createdby'] = $this->application->auth->user['user_id'];
            else
                $data['location_modifiedby'] = $this->application->auth->user['user_id'];

            $model = new Classes_Location( $data );

            if( is_numeric( $location_id = $model->save()->location_id ) )
            {
                // Rimanda alla pagina di associazione dei contatti...
                header( 'Location: /admin/location/associacontatti/id/' . $location_id );
            }
            else
            {
                $errorMsg = $model->errors;

                if( $model->errors['location_name'] )
                    $errorMsg['location_address'] = sprintf( ERR_FORMAT, 'Nome Location' );

                if( $model->errors['location_city'] )
                    $errorMsg['location_address'] = sprintf( ERR_EMPTY_FIELD, 'Citt&agrave;' );

                if( $model->errors['location_cap'] )
                    $errorMsg['location_address'] = sprintf( ERR_FORMAT, 'Cap' );

                if( $model->errors['location_province_id'] )
                    $errorMsg['location_address'] = sprintf( ERR_EMPTY_SELECT, 'Provincia' );
            }          
	}
        
        $this->layout->errorMsg = $errorMsg;
	$this->layout->content	= array( 'script' => 'location/info' );
    }

    public function archivioAction()
    {
	$this->layout->locations = $this->model_location->fetchAll();
	$this->layout->content = array( 'script' => 'location/archivio' );
    }    
    
    public function riepilogoAction()
    {
	if( empty( $this->location ) )
	    trigger_error( 'Nessuna Location Selezionata' );

        # Assegniamo i dati al layout
        $this->layout->content = array( 'script' => 'location/riepilogo' );
    }      
    
    public function vediAction()
    {
	if( empty( $this->location ) )
	    trigger_error( 'Nessuna Location Selezionata' );
	
	$this->layout->content	= array( 'script' => 'location/vedi' );		
    }
    
    public function associacontattiAction()
    {
	if( !is_array( $this->location ) )
            return header( 'Location: /admin/location/info' );
        
	if( $this->form_data['salva'] AND $this->form_data['salva'] !== '' )
	{
	    $roles = array();
	    
            if( count( $this->form_data['contacts'] ) > 0 )
            {
                foreach( $this->form_data['contacts'] AS $key => $value )
                {
                    foreach( $value as $role ){
                        // Mapping...
                        $roles[] = array( 'fab_contacts_contact_id' => $key, 'fab_contacts_role' => $role );
                        
                    }
                }
            }
	    
	    if( !$errorMsg )
	    {
		$model = new Classes_Location( array( 'location_id' => $this->location['location_id'] ) );
		
                if( is_numeric( $location_id = $model->saveExtra( array( 'contacts' => $roles ) )->location_id ) AND !$model->errors )
                {
                    // Redirect...
                    return header( 'Location: /admin/location/riepilogo/id/' . $location_id );
                }
                else
                {
                    if( $model->errors )
                        $errorMsg = $model->errors;
                }
	    }
	}        

	$this->layout->errorMsg = $errorMsg;
	$this->layout->content	= array( 'script' => 'location/associa-contatti' );	
    }
}
